package com.xssn.fjnd.content.controller;


import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.extension.api.R;
import com.xssn.fjnd.content.req.AdvertREQ;
import com.xssn.fjnd.content.req.VideoREQ;
import com.xssn.fjnd.content.service.IVideoService;
import com.xssn.fjnd.entities.Video;
import com.xssn.util.base.Result;
import com.xssn.util.exception.ExceptionUtils;
import com.xssn.util.exception.XssnException;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 视频信息表 前端控制器
 * </p>
 *
 * @author 线狮少年
 * @since 2023-08-14
 */

@Api(value = "阿里云视频点播微服务", description = "阿里云视频点播微服务, 提供演出视频的上传删除，增删改查")
@RestController
@Slf4j
@RequestMapping("/video")
public class VideoController {

    @Autowired
    private IVideoService videoService;

    @ApiOperation(value = "上传阿里云视频")
    @PostMapping("upload")
    public Result uploadVideo(
            @ApiParam(name = "file", value = "文件", required = true)
            @RequestParam("file") MultipartFile file
    ) {
        String videoId = videoService.uploadVideo(file);
        return Result.ok(videoId);
    }


    @ApiOperation(value = "根据id删除阿里云视频")
    @DeleteMapping("deleteVideoBySourceId/{videoId}")
    public Result deleteVideoBySourceId(@ApiParam(name = "videoId", value = "云端视频id", required = true)
                         @PathVariable String videoId) {

        return videoService.deleteVideoBySourceId(videoId);

    }


    /**
     * 批量删除视频
     *
     * @param videoIdList
     * @return
     */
    @ApiOperation(value = "根据id列表删除阿里云视频")
    @DeleteMapping("remove")
    public Result removeVideoBySourceIdList(
            @ApiParam(value = "阿里云视频id列表", required = true)
            @RequestBody List<String> videoIdList) {
        try {
            videoService.removeVideoByIdList(videoIdList);
            return Result.ok();
        } catch (Exception e) {
            log.error(ExceptionUtils.getMessage(e));
            throw new XssnException(20001, e.getMessage());
        }
    }



    @ApiOperation("通过视频id获取视频凭证PlayAuth")
    @GetMapping("getPlayAuth/{videoSourceId}")
    public Result getPlayAuth(
            @ApiParam(value = "阿里云视频文件的id", required = true)
            @PathVariable String videoSourceId) {

        try {
            String playAuth = videoService.getPlayAuth(videoSourceId);
            return Result.ok("获取播放凭证成功", playAuth);

        } catch (Exception e) {
            log.error(ExceptionUtils.getMessage(e));
            throw new XssnException(20001, "视频凭证获取失败");
        }
    }

    @ApiOperation("通过视频id获取视频地址PlayUrl")
    @GetMapping("getPlayUrl/{videoSourceId}")
    public Result getPlayUrl(
            @ApiParam(value = "阿里云视频文件的id", required = true)
            @PathVariable String videoSourceId) {

        try {
            String playInfo = videoService.getPlayInfo(videoSourceId);
            return Result.ok("获取播放地址成功", playInfo);

        } catch (Exception e) {
            log.error(ExceptionUtils.getMessage(e));
            throw new XssnException(20001, "视频地址获取失败");
        }
    }



    @PreAuthorize("hasAuthority('video:search')")
    @ApiOperation("根据视频标题和视频类型查询视频分页列表接口")
    @PostMapping("/search") // /content/video/search
    public Result search(@RequestBody VideoREQ req) {
        return videoService.queryPage(req);
    }


    @PreAuthorize("hasAuthority('video:add')")
    @ApiOperation(value = "新增视频")
    @PostMapping("addVideo")
    public Result addVideo(@RequestBody Video video) {
        videoService.saveVideoInfo(video);
        return Result.ok();
    }

//    @PreAuthorize("hasAuthority('video:delete')")
    @ApiOperation(value = "删除视频对象")
    @DeleteMapping("deleteVideoById/{id}")
    public Result deleteVideoById(@PathVariable("id") String id) {
        return videoService.deleteVideoById(id);
    }



    @ApiOperation(value = "根据ID查询视频详情")
    @GetMapping("getVideoInfo/{id}")
    public Result getVideInfoById(@PathVariable("id") String id) throws ClientException {

        Video video = videoService.getById(id);
        String playInfo = videoService.getPlayInfo(video.getVideoSourceId());
        video.setTemporaryUrl(playInfo);
        video.setTemporaryAuth(videoService.getPlayAuth(video.getVideoSourceId()));
        return Result.ok(video);

    }

    @PreAuthorize("hasAuthority('video:edit')")
    @ApiOperation(value = "修改视频详情")
    @PutMapping
    public Result update(@RequestBody Video video)  {
        videoService.updateVideoInfoById(video);
        return Result.ok();
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "视频ID", required = true),
            @ApiImplicitParam(name = "count", value = "点赞数", required = true)
    })
    @ApiOperation("更新点赞数")
    @PutMapping("/thumb/{id}/{count}")
    public Result updataThumhup(@PathVariable("id") String id,
                                @PathVariable("count") int count) {
        return videoService.updateThumhup(id, count);
    }


}
